package com.starchen.optimalselection.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starchen.optimalselection.web.mapper.MemberAddressMapper;
import com.starchen.optimalselection.web.model.MemberAddress;
import com.starchen.optimalselection.web.service.MemberAddressService;
import com.starchen.optimalselection.web.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户地址管理Service实现类
 */
@Service
public class MemberAddressServiceImpl extends ServiceImpl<MemberAddressMapper, MemberAddress>  implements MemberAddressService {

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberAddressMapper addressMapper;

    @Override
    public int add(MemberAddress address) {
        return addressMapper.insert(address);
    }

    @Override
    public void delete(Long id) {
        QueryWrapper<MemberAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        addressMapper.delete(queryWrapper);
    }

    @Override
    public void update(MemberAddress address) {

//        //example.createCriteria().andMemberIdEqualTo(currentMember.getId()).andIdEqualTo(id);
//
//        if(address.getDefaultStatus()==1){
//            //先将原来的默认地址去除
//            QueryWrapper<MemberAddress> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("member_id",address.getMemberId());
//            queryWrapper.eq("default_status",1);
//
//            addressMapper.updateByExampleSelective(record,updateExample);
//        }
//         addressMapper.updateByExampleSelective(address,example);
    }

    @Override
    public List<MemberAddress> list(Long memberId) {
        QueryWrapper<MemberAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id",memberId);
        List<MemberAddress> list = addressMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public MemberAddress getItem(Long id) {
        QueryWrapper<MemberAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        MemberAddress address = addressMapper.selectById(id);
        return address;
    }
}
